using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using GarageGames.Torque.GUI;
using GarageGames.Torque.Sim;
using GarageGames.Torque.GameUtil;
using GarageGames.Torque.Core.Xml;
using GarageGames.Torque.Core;
using GarageGames.Torque.T2D;
using Xyne.Events;
using Xyne.Utilities;

namespace Xyne.GUI
{
    class CanvasStateMachine
    {
        /// <summary>
        /// Defines the differen possible CanvasStates
        /// </summary>
        public enum CanvasStates
        {
            NotStarted,
            SplashScreen,
            MainMenu,
            MainGame,
            EndGame
        };

        private static CanvasStateMachine instance = new CanvasStateMachine();
        private CanvasStates currentState = CanvasStates.NotStarted;
        private CanvasStates nextState = CanvasStates.SplashScreen;

        private SplashScreenGUI splashScreen;
        private MainGameGUI mainGameScreen;
        private MainMenuGUI mainMenuScreen;


        /// <summary>
        /// Sets the next state the canvas should be in on the next update.  This is
        /// how you change what is displayed to the user.
        /// </summary>
        public CanvasStates NextCanvasState
        {
            set 
            { 
                nextState = value;
                Update();
            }
        }

        public static CanvasStateMachine Instance
        {
            get { return instance; }
        }

        private CanvasStateMachine()
        {
            Game.Instance.SceneLoader.OnSceneUnloaded = new SceneLoader.OnSceneUnloadedDelegate(OnSceneUnloaded);
        }

        /// <summary>
        /// Initializes all of the static GUI menus that are defined within this namespace
        /// </summary>
        public void CreateGUI()
        {
            EnterNextCanvasState();
        }

        public void Update()
        {
            // Check if the Canvas needs to be changed
            if (currentState != nextState)
            {
                ExitPreviousCanvasState();
                EnterNextCanvasState();
            }
        }

        private void EnterNextCanvasState()
        {
            switch (nextState)
            {
                case CanvasStates.SplashScreen:
                    splashScreen = new SplashScreenGUI();
                    GUICanvas.Instance.SetContentControl("SplashScreen");
                    break;
                case CanvasStates.MainMenu:
                    mainMenuScreen = new MainMenuGUI();
                    GUICanvas.Instance.SetContentControl("MainMenu");
                    break;
                case CanvasStates.MainGame:
                    Game.Instance.SceneLoader.OnSceneLoaded = new SceneLoader.OnSceneLoadedDelegate(OnSceneLoaded);
                    Game.Instance.SceneLoader.Load(@"data\scenes\main.txscene");
                    break;
                case CanvasStates.EndGame:
                    break;
            }

            currentState = nextState;
        }

        private void ExitPreviousCanvasState()
        {

            Game.Instance.SceneLoader.UnloadLastScene();
            switch (currentState)
            {
                case CanvasStates.NotStarted:
                    break;
                case CanvasStates.SplashScreen:
                    break;
                case CanvasStates.MainMenu:
                    break;
                case CanvasStates.MainGame:
                    break;
            }
        }

        private void OnSceneUnloaded(string sceneName, TorqueSceneData sceneData)
        {
            TorqueSceneData scene = TorqueObjectDatabase.Instance.FindObject<TorqueSceneData>();
        }

        private void OnSceneLoaded(string sceneName, TorqueSceneData sceneData)
        {
            mainGameScreen = new MainGameGUI();
            mainGameScreen.InitializeUI();
            GUICanvas.Instance.SetContentControl("MainGame");
        }
    }
}
